<?php

/**
 * product actions.
 *
 * @package    samsung
 * @subpackage product
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class productActions extends sfActions
{

  /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeList(sfWebRequest $request) {
    $this->products = PRODUCTTable::getInstance()->getProducts(false);
    $this->arrStatusList    = StringUtil::getStatusArray();
  }

  public function executeAdd(sfWebRequest $request) {
    $errorCollector = array();
    $this->form = new ProductForm();
    $this->isEdit = false;
    
    if ($request->hasParameter('id')) {
      $this->isEdit = true;
      $product = PRODUCTTable::getInstance()->find($request->getParameter('id'));
      $this->form->populateData($product);
      if (!$product instanceof PRODUCT) {
        $this->isEdit = false;
      }
    }
    try {
      if ($request->isMethod('post')) {
        $formData = $request->getParameter(ProductForm::FORM_NAME);
        $formFiles = $request->getFiles(ProductForm::FORM_NAME);
        $this->form->bind($formData, $formFiles);

        if ($this->form->isValid()) {
          if (!$this->isEdit) {
            $product = new PRODUCT();
            $lineNo = 1;
            $product->setLineNo($lineNo);
          }
          $product->setProductName($formData[ProductForm::FIELD_NAME]);
          $product->setDescription($formData[ProductForm::FIELD_DESCRIPTION]);
          $product->setIsActive($formData[ProductForm::FIELD_STATUS]);
          $product->save();

          if (is_uploaded_file($_FILES[ProductForm::FORM_NAME]['tmp_name'][ProductForm::FIELD_IMAGE])) {
            $fileName = $_FILES[ProductForm::FORM_NAME]['name'][ProductForm::FIELD_IMAGE];
            $fileArr  = explode('.', $fileName);
            $extention = $fileArr[count($fileArr)-1];
            $source = $_FILES[ProductForm::FORM_NAME]['tmp_name'][ProductForm::FIELD_IMAGE];
            $destination = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'product'.DIRECTORY_SEPARATOR;
            $destination = $destination.$product->getId().'.'.$extention;

            if (move_uploaded_file($_FILES[ProductForm::FORM_NAME]['tmp_name'][ProductForm::FIELD_IMAGE], $destination)) {
              if (file_exists($destination)) {
                ImageUtil::resizeImage($destination);
              }
            }
            $product->setImageExt($extention);
            $product->save();
          }

          $this->redirect('@product-list');
        }
      }
    } catch (Exception $e) {
      $errorCollector[] = 'Internal error!.. Please try again';
      error_log($e->getMessage());
    }
    $this->errorCollector = $errorCollector;
  }

  public function executeDelete(sfWebRequest $request) {
    $product = PRODUCTTable::getInstance()->find($request->getParameter('id'));
    if (!$product instanceof PRODUCT) {
      $this->forward404();
    }
    try {
      $product->setIsActive(Constants::STATUS_DELETED);
      $product->save();

      $this->redirect('@product-list');
    } catch (Exception $e) {

    }
  }
    
}
